/**
 * Copyright 2018 asiainfo Inc.
 **/
package com.myz.service.memeber.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author maoyz on 2018/9/7
 * @version: v1.0
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String port;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String hello() {
        System.out.println("**************** index ************************" + LocalDateTime.now());
        try {
            // 模拟业务延迟
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 服务发现
        List<ServiceInstance> instances = discoveryClient.getInstances("service-member");
        if (!CollectionUtils.isEmpty(instances)) {
            StringBuilder sb = new StringBuilder();
            sb.append(instances.get(0).getServiceId()).append(" ");
            for (ServiceInstance instance : instances) {
                sb.append(instance.getHost()).append(" : ").append(instance.getPort()).append(" ;");
            }
            System.out.println(sb);
        }

        return "hello World : " + port + " : " + LocalDateTime.now();
    }

    /**
     * 测试 service-feign 调用member服务，其中service-feign 携带头部信息
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public String get(@PathVariable("id") Integer id, HttpServletRequest request) {
        System.out.println(request.getHeader("Authorization"));
        return id.toString();
    }

    /**
     * 测试 service-feign 调用member服务，显示传递header信息
     *
     * @param username
     * @param request
     * @return
     */
    @RequestMapping(value = "/header", method = RequestMethod.POST)
    public String header(@RequestParam(value = "username") String username, HttpServletRequest request) {
        System.out.println("username = " + username);
        System.out.println(request.getHeader("Authorization"));
        try {
            // 模拟业务延迟
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "header";
    }

    /**
     * 测试 service-feign 调用member服务，服务异常
     *
     * @param username
     * @param request
     * @return
     */
    @RequestMapping(value = "/error0", method = RequestMethod.GET)
    public String error(@RequestParam(value = "username") String username, HttpServletRequest request) {
        if ("abc".equals(username)) {
            throw new RuntimeException("error abd");
        }
        return username;
    }

    /**
     * 测试 service-feign 的文件上传服务
     *
     * @param file 上传文件
     * @return success
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public String upload(@RequestPart(value = "file") MultipartFile file) {
        System.out.println("upload fileNme = " + file.getOriginalFilename());
        return file.getOriginalFilename();
    }
}
